from rest_framework import serializers
from apps.headline.models import Article, Channel
from apps.headline.serializers.channels import ChannelModelSerializer
from apps.headline.serializers.collected_users import CollectUserModelSerializer
from apps.headline.serializers.comments import CommentModelSerializer
from apps.headline.serializers.labels import LabelModelSerializer
from apps.headline.serializers.user import UserModelSerializer
# from apps.users.models import User
from apps.question.models import Label
from apps.users.models import User


class ArticlesModelSerializer(serializers.ModelSerializer):
    user = UserModelSerializer()
    collected_users = CollectUserModelSerializer(many=True)
    collected = serializers.SerializerMethodField()

    class Meta:
        model = Article
        fields = '__all__'

    def get_collected(self, object):
        # article = object
        # users = article.collected_users.all()
        # for obj in users:
        #     # 获取当前登录用户
        #     # user = self.context.get('user')
        #     # 如果当前登录用户为默认用户
        #     if user is None:
        #         return False
        #     # 如果当前登录用户为收藏用户
        #     elif user == obj:
        #         return True
        # else:
        #     return False
        return False
    # def validate(self, attrs):
    #     data = attrs
    #     return attrs


class ArticlesCreateFindSerializer(serializers.ModelSerializer):
    # labels = LabelModelSerializer(many=True)
    # user = UserModelSerializer()
    # comments = CommentModelSerializer(many=True)
    # # collected_users = CollectUserModelSerializer(many=True)
    # channel = ChannelModelSerializer()

    class Meta:
        model = Article
        fields = '__all__'

    # def create(self, validated_data):
    #     # labels = validated_data.pop('labels')
    #     # collected_users = validated_data.pop('collected_users')
    #     comments = validated_data.pop('comments')
    #     user = validated_data.pop('user')
    #     channel = validated_data.pop('channel')
    #     c_id = channel.get('id')
    #     channel_obj = Channel.objects.get(id=c_id)
    #     article = Article.objects.create(**validated_data)
    #     article.channel = channel_obj
    #     article.save()
    #     # article.save(channel=channel)
    #     # user 获取
    #     for item in labels:
    #         labels_id = item.get('id')
    #         label = Label.objects.get(id=labels_id)
    #         article.add(label)
    #         label.add(article)
    #         label.save()
    #     return article



